/**
 * Created by CodeTlan.
 * User: Ing. Miguel Salas
 * Date: 3/29/12
 * Time: 12:04 AM
 */
Ext.define('CT.controller.Historiales', {
    extend: 'Ext.app.Controller',
    views:[
        'historial.Window',
        'historial.Grid',
        'historial.Form',
        'historial.View',
        'historial.PanelFoto',
        'historial.WinFoto',
        'historial.FormFoto'
    ],
    stores:[
        'Historiales',
        'Fotos'
    ],
    models:[
        'Historial',
        'Foto'
    ],
    requires: [
        'Ctc.view.DateMX'
    ],
    refs:[{
        ref:'viewFotos',
        selector:'viewfotos'
    }],
    win:false,
    init:function(){
        this.control({
            'formhistorial button[action=add]': {
                click: this.agregar
            },
            'gridhistorial':{
                cellclick:this.clicked,
                edit:this.editar,
                select:this.viewFotos
            },
            'gridhistorial checkcolumn':{
                checkchange:this.activar
            },
            'panelfoto button[action=addFotos]': {
                click: this.btnWinFoto
            },
            'formfoto button[action=addFoto]': {
                click: this.agregarFoto
            },
            'viewfotos':{
                itemclick:this.eliminarFoto
            }
        });
    },
    lanzarVentana:function(anim,icon){
        if(!this.win){
            this.init();
            this.win=Ext.widget('winhistoriales',{animateTarget:anim,iconCls:icon});
        }
        this.win.show();
    },
    clicked:function(grid, cell, index, record){
        switch(index){
            case 5:this.eliminar(grid,record.data);break;
        }
    },
    viewFotos:function(model,selected){
        var win=model.view.up("window");
        var panelFotos=win.down('#panelFotos');
        var viewFotos= panelFotos.down('#idViewFotos');

        panelFotos.expand();

        this.historial_k=selected.data.historial_k;

        viewFotos.store.setParams({'historial_k':this.historial_k},true);



    },
    agregar:function(button){
        var form = button.up('form').getForm();
        var store=this.getHistorialesStore();
        if (form.isValid()) {
            form.submit({
                url:'index.php/historiales/add',
                success: function() {
                    form.reset();
                    Notification.ok('Historial agregado', ' ');
                    store.load();
                }
            });
        }
    },

    agregarFoto:function(button){
        var form = button.up('form').getForm();
        var win = button.up('window');
        var store=this.getFotosStore();
        var viewFotos=this.getViewFotos();
        if (form.isValid()) {
            form.submit({
                url:'index.php/historiales/addfoto',
                params:{ historial_k : this.historial_k },
                success: function(action,result) {

                    win.destroy();

                    Notification.ok('Foto agregada', ' ');
                    store.load();


                },
                failure:function(){
                    Notification.error('La foto debe de estar en formato JPG', ' ');
                }
            });
        }
    },

    editar:function(editor, e){

        var valor=Ext.isDate(e.value) ? Ext.util.Format.date(e.value, 'Y-m-d') : e.value;

        var mods='{"'+e.field+'":"'+valor+'"}';
        Ext.Ajax.request({
            url: 'index.php/historiales/edit',
            params: {
                mod: mods,
                historial_k: e.record.data.historial_k
            },
            success: function(response){
                e.record.commit();
                Notification.ok('Historial Modificado', ' ');
            }
        });
    },

    activar:function(check, index, activo){
        var store = this.getHistorialesStore();
        var record = store.getAt(index);
        Ext.Ajax.request({
            url: 'index.php/historiales/active',
            params: {
                historial_k: record.data.historial_k,
                activo: activo?1:0
            },
            success: function(response){
                record.commit();
                Notification.ok('Historial Modificado', ' ');
            }
        });
    },
    eliminar:function(grid,record){
        Ext.Msg.confirm('Acción requerida','Está seguro que desea eliminar el historial: '+record.titulo+'?',function(r){
            if(r=="yes"){
                var store=this.getHistorialesStore();
                Ext.Ajax.request({
                    url: 'index.php/historiales/delete',
                    params: {
                        historial_k: record.historial_k
                    },
                    success: function(response){
                        var sm = grid.getSelectionModel();
                        store.remove(sm.getSelection());
                        Notification.ok('Historial Eliminado',' ');
                    }
                });
            }
        },this);
    },
    eliminarFoto:function(view, record, index, e, o){
        Ext.Msg.confirm('Acción requerida','Está seguro que desea eliminar esta foto ?',function(r){
            if(r=="yes"){
                var store=this.getFotosStore();
                Ext.Ajax.request({
                    url: 'index.php/historiales/deletefoto',
                    params: {
                        foto_k: record.data.foto_k
                    },
                    success: function(response){
                        var sm = view.getSelectionModel();
                        //store.remove(sm.getSelection());
                        store.load();
                        Notification.ok('Foto eliminada',' ');
                    }
                });
            }
        },this);
    },
    btnWinFoto:function(button){
        var win=Ext.widget('winfoto',{iconCls:'add'});
        win.show();
    }
});